<template>
  <view class="product-wrap">
    <view class="select">
      <uni-icons
        :type="product.selected ? 'checkbox-filled' : 'circle'"
        size="24"
        :color="product.selected ? '#222' : '#aaa'"
        @click="selectProduct(product.cart_id, product.selected)"
      ></uni-icons>
    </view>
    <view class="product" @click="viewProduct(product.product_id)">
      <view class="product-img">
        <image :src="product.image_url" mode="aspectFill" />
      </view>
      <view class="product-info">
        <view class="product-title">{{ product.name }}</view>
        <view class="variant" v-if="product.variant_labels">{{ product.variant_labels }}</view>
        <view class="product-price-now">{{ product.price_format }}</view>
        <view class="product-bottom">
          <view class="product-number-box" @click.stop="">
            <uni-number-box
              v-model="product.quantity"
              @change="
                e => {
                  quantityChange(e, product.cart_id, product.sku_id)
                }
              "
              :min="product.minimum"
            />
          </view>
          <view class="delete" @click.stop="productDelete(product.cart_id)">
            <uni-icons type="trash" size="18"></uni-icons>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
// const emit = defineEmits(['change'])
import { t } from '@/locale/index'
const props = defineProps({
  product: {
    type: Object,
    default: () => {},
  },
})

const { proxy } = getCurrentInstance()

const viewProduct = product_id => {
  uni.navigateTo({
    url: `/pages/product/product?id=${product_id}`,
  })
}

const selectProduct = (cart_id, selected) => {
  proxy.$http.put(`carts/${selected ? 'unselect' : 'select'}`, {cart_ids: [cart_id]}).then(res => {
    // proxy.$util.msg({ title: res.message, icon: 'success' })
    proxy.$emit('change', res.data)
  })
}

const quantityChange = (quantity, cart_id, sku_id) => {
  proxy.$http.put(`carts/${cart_id}`, { sku_id, quantity }).then(res => {
    // proxy.$util.msg({ title: res.message, icon: 'success' })
    proxy.$emit('change', res.data)
  })
}

const productDelete = cart_id => {
  const self = this

  proxy.$http.del(`carts/${cart_id}`).then(res => {
    proxy.$emit('change', res.data)
  })
}
</script>

<style lang="scss" scoped>
.product-wrap {
  padding: 20rpx;
  background-color: #fff;
  margin-top: 20rpx;
  border-radius: 10rpx;
  display: flex;
  align-items: center;

  .select {
    margin-right: 20rpx;
  }

  .product {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex: 1;

    .product-img {
      width: 160rpx;
      height: 160rpx;
      margin-right: 20rpx;
      border-radius: 10rpx;
      overflow: hidden;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .product-info {
      flex: 1;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .product-title {
        font-size: 28rpx;
        color: #333;
        font-weight: bold;
        width: 200px;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
        overflow: hidden;
        margin-bottom: 10rpx;
      }

      .variant {
        color: #666;
        font-size: 26rpx;
      }

      .product-price-now {
        font-size: 32rpx;
        font-weight: bold;
        color: $primary;
        margin-top: 10rpx;
      }

      .product-bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 10rpx;

        .delete {
          width: 60rpx;
          height: 60rpx;
          border-radius: 50%;
          background-color: #eee;
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    }
  }
}
</style>
